package com.dk.wiki.service;

import com.dk.wiki.domain.Category;
import com.dk.wiki.domain.CategoryExample;
import com.dk.wiki.mapper.CategoryMapper;
import com.dk.wiki.req.CategoryQueryReq;
import com.dk.wiki.req.CategorySaveReq;
import com.dk.wiki.resp.CategoryQueryResp;
import com.dk.wiki.resp.PageResp;
import com.dk.wiki.util.CopyUtil;
import com.dk.wiki.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: ldk
 * 描述: TODO
 */
@Service
public class CategoryService {

    private static final Logger LOG = LoggerFactory.getLogger(CategoryService.class);

    @Resource // jdk所带的注解
    // @Autowired springboot所带的注解
    private CategoryMapper categoryMapper;

    @Resource
     private SnowFlake snowFlake;

    // 根据单一参数，返回查询数据
    // public List<Category> list(String name){
    //     CategoryExample categoryExample = new CategoryExample();
    //     CategoryExample.Criteria criteria = categoryExample.createCriteria();
    //     criteria.andNameLike("%" + name + "%");
    //     List<Category> categorys = categoryMapper.selectByExample(categoryExample);
    //     return categorys;
    // }

    public List<CategoryQueryResp> all(){
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);

        // 列表复制
        List<CategoryQueryResp> categoryQueryRespList = CopyUtil.copyList(categoryList, CategoryQueryResp.class);
        return categoryQueryRespList;
    }

    public PageResp<CategoryQueryResp> list(CategoryQueryReq categoryQueryReq){
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        CategoryExample.Criteria criteria = categoryExample.createCriteria();

        PageHelper.startPage(categoryQueryReq.getPage(), categoryQueryReq.getSize());
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);

        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        LOG.info("总行数：{}",pageInfo.getTotal());
        LOG.info("总页数：{}",pageInfo.getPages());
        // List<CategoryResp> categoryRespList = new ArrayList<>();
        // for (Category category : categorysList) {
        //     // CategoryResp categoryResp = new CategoryResp();
        //     // // 逐一更行赋值，比较慢
        //     // // categoryResp.setId(category.getId());
        //     // // 使用spring的BeanUtils工具
        //     // BeanUtils.copyProperties(category, categoryResp);
        //     CategoryResp categoryResp = CopyUtil.copy(category, CategoryResp.class);
        //     categoryRespList.add(categoryResp);
        // }

        // 列表复制
        List<CategoryQueryResp> categoryQueryRespList = CopyUtil.copyList(categoryList, CategoryQueryResp.class);
        PageResp<CategoryQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(categoryQueryRespList);
        return pageResp;
    }

    /**
     * 保存
     * @param categorySaveReq
     */
    public  void save(CategorySaveReq categorySaveReq){
        Category category = CopyUtil.copy(categorySaveReq, Category.class);
        if (ObjectUtils.isEmpty(categorySaveReq.getId())){
            // 新增
            category.setId(snowFlake.nextId());
            categoryMapper.insert(category);
        }else{
            // 更新
            categoryMapper.updateByPrimaryKey(category);
        }
    }

    /**
     * 删除
     * @param id
     */
    public  void delete(Long id){
        categoryMapper.deleteByPrimaryKey(id);
    }

}
